Skip to content

gh-61745: Clarify os.listdir behavior for empty path#145338

Open
u-wlkjyy wants to merge 1 commit intopython:mainfrom
u-wlkjyy:fix-61745-listdir-empty-path
Open

gh-61745: Clarify os.listdir behavior for empty path#145338
u-wlkjyy wants to merge 1 commit intopython:mainfrom
u-wlkjyy:fix-61745-listdir-empty-path

Conversation

@u-wlkjyy
Copy link

@u-wlkjyy u-wlkjyy commented Feb 28, 2026

Fixes gh-61745.

Clarify the os.listdir() docs to explicitly state that passing an empty
string ('') raises FileNotFoundError, and that '.' should be used
for the current directory.

This change updates documentation only.


📚 Documentation preview 📚: https://cpython-previews--145338.org.readthedocs.build/

@python-cla-bot
Copy link

python-cla-bot bot commented Feb 28, 2026

All commit authors signed the Contributor License Agreement.

CLA signed

If a file is removed from or added to the directory during the call of
this function, whether a name for that file be included is unspecified.
*path* must refer to an existing directory. Passing ``''`` raises
:exc:`FileNotFoundError`; use ``'.'`` to list the current directory.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to suggest omitting the path argument to list the current directory.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion. I’d prefer to keep '.' in the docs here.
Omitting the argument is a convenience specific to os.listdir(), while '.' is the explicit path value used across many path APIs. Keeping '.' also makes the contrast with '' clearer: '' raises FileNotFoundError, but '.' means the current directory.
For example, developers explicitly use os.stat('.'), os.path.abspath('.'), and pathlib.Path('.') to refer to the current directory. In those APIs, you can’t express the same intent by omitting an argument. Using '.' here reduces context switching and avoids confusion for readers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review docs Documentation in the Doc dir skip news

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

[doc] os.listdir and os.path.join inconsistent on empty path

3 participants